<template>
  <div class="app-container">
    <div style="font-size: 25px;">销售退货单</div>
    <el-divider></el-divider>
    <el-form ref="form" :model="form" label-width="80px">
      <el-col :span="8">
        <el-form-item label="销售单号" prop="saleNumber" style="width: 400px"
                      :rules="[{ required: true ,message: '请输入销售单号'}]">
          <el-input v-model="form.saleNumber" placeholder="请输入销售单号" maxlength="15" required="required" disabled/>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="发货单号" style="width: 400px" prop="shipmentNum">
          <el-input v-model="shipmentNo"
                    placeholder="发货单号"
                    maxlength="15"
                    :disabled="true"/>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="客户" prop="customerId" style="width: 400px" :rules="[{ required: true ,message: '请选择客户'}]">
          <el-select
            disabled
            v-model="form.customerId"
            placeholder="请选择客户"
            clearable
            size="small"
            style="width: 100%"
            @change="getInfo(form.customerId)"
          >
            <el-option
              v-for="dict in customerList"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="客户手机" style="width: 400px" prop="customerPhone">
          <el-input disabled v-model="customerTelephone" placeholder="请输入客户手机"/>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="收获地址" style="width: 400px" prop="customerAddress">
          <el-cascader
            disabled
            style="width: 100%"
            expand-trigger="hover"
            ref="cascaderAddr"
            size="small"
            :options="options"
            v-model="selectedOptions"
            >
            <template slot-scope="{ node, data }">
              <span>{{ data.label }}</span>
              <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
            </template>
          </el-cascader>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="详细地址" style="width: 400px" prop="detailedAddress">
          <el-input v-model="detailedAdd" disabled placeholder="请输入详细地址"/>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="固定电话" style="width: 400px" prop="fixedTelephone">
          <el-input v-model="form.fixedTelephone" disabled placeholder="请输入固定电话"/>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="收款方式" style="width: 400px" prop="paymentMethod">
          <el-select v-model="form.paymentMethod" disabled placeholder="请选择收款方式" style="width: 100%" clearable>
            <el-option
              v-for="dict in dict.type.erp_payment_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="备注" style="width: 400px" prop="customerContact">
          <el-input v-model="form.customerContact" disabled placeholder="请输入简要备注" maxlength="255"/>
        </el-form-item>
      </el-col>

      <el-row>
        <el-col :span="8">
          <el-form-item label="发货状态" style="width: 400px">
            <span v-if="textStatus" style="display:inline-block;width:200px;font-weight:bold;">
            {{ auditStatus }}
          </span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row>
      <el-table :data="addGoodsList" default-expand-all
                :row-class-name="getRowClassName">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-table v-show="props.row.goodWarehouseList.length != 0" v-bind:data="props.row.goodWarehouseList"
                      :default-expand-all="true"  :show-header="true"
                      :header-cell-style="{background:'#FFFFFF',color:'#606266'}">
              <el-table-column prop="warehouseName" align="center" label="发货仓库"/>
              <el-table-column prop="goodsNumber" align="center" label="发货数量"/>
              <el-table-column prop="returnGoodNumber" align="center" label="退货数量"/>
              <el-table-column prop="returnGoodAmount" align="center" label="退货金额"/>
            </el-table>
          </template>
        </el-table-column>
        <el-table-column label="商品ID" align="center" prop="id"/>
        <el-table-column label="商品名称" align="center" prop="name"/>
        <el-table-column label="分类" align="center" prop="categoryName"/>
        <el-table-column label="品牌" align="center" prop="brandName"/>
        <el-table-column label="单位" align="center" prop="companyName"/>
        <el-table-column label="成本单价" align="center" prop="cost"/>
        <el-table-column label="销售单价" align="center" prop="price">
          <div class="item" slot-scope="scope">
            <el-input class="item__input" disabled v-model="scope.row.price" placeholder="请输入价格" size="mini"
                      style="width:100px" ></el-input>
          </div>
        </el-table-column>
        <el-table-column label="销售数量" align="center" prop="quantity">
          <div class="item" slot-scope="scope">
            <el-input-number v-model="scope.row.quantity" disabled controls-position="right" :min="0" size="mini"
                             style="width:100px" />
          </div>
        </el-table-column>
        <el-table-column label="销售总价" align="center" prop="oneGoodPrice">
          <div class="item" slot-scope="scope">
            <el-input disabled v-model="scope.row.oneGoodPrice" placeholder="销售总价" size="mini"
                      style="width:100px"></el-input>
          </div>
        </el-table-column>

      </el-table>
    </el-row>

    <div slot="footer" class="dialog-footer" style="text-align:center;margin-top: 20px;">
      <el-button type="primary" @click="confirmReceipt" v-show="this.isReturn == 1">确认商品入库</el-button>
      <el-button type="primary" @click="close" v-show="this.isReturn == 0">返回</el-button>
    </div>
  </div>

</template>

<script>

import {listCustomer} from "@/api/erp/customer";
import {regionData} from "element-china-area-data";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {getSale} from "@/api/erp/sale";
import {returnGood} from "@/api/erp/return";

let stream = require('stream')

export default {
  name: "sale",
  components: {Treeselect},
  dicts: ['erp_payment_type'],
  data() {
    return {
      //退货商品列表
      returnGoodList: [],
      //退货商品是否入库
      isReturn: null,
      //发货单号
      shipmentNo: null,
      //商品单价
      itemPricing: null,
      //订单状态文字状态
      textStatus: false,
      //订单状态
      auditStatus: null,
      //仓库数据列表
      warehouseGoodsList: [],
      //销售id
      sellId: null,
      //品牌列表
      brandList: [],
      //elementUi地区数据
      options: regionData,
      //分类列表
      categoryOptions: [],
      //客户列表
      customerList: [],
      //客户电话
      customerTelephone: null,
      //客户地址
      detailedAdd: null,
      // 商品信息表格数据
      goodsList: [],
      // 添加的商品信息表格数据
      addGoodsList: [],
      //客户省市区
      selectedOptions: [],
      //表单参数
      form: {},
      //商品表单参数
      goodsForm: {},
      // 遮罩层
      loading: true,
    };
  },
  created() {
    const saleId = this.$route.query.id
    const type = this.$route.query.isReturn
    this.isReturn = type
    this.sellId = saleId;
    if (saleId) {
      this.getList(saleId);
    }
    this.selectCustomer();
  },

  methods: {
    //确认退货商品入库方法
    confirmReceipt() {
      let info = this.addGoodsList
      let _that = this
      this.$modal.confirm('是否确认改订单已无其他退货商品!').then(function () {
        returnGood(info).then(res => {
          if (res.code == 200) {
            _that.$modal.msgSuccess("入库成功!")
            _that.close();
          }
        })
      })
    },
    //刷新页面数据
    getList(saleId) {
      getSale(saleId).then(response => {
        this.form = response.data
        this.customerTelephone = response.data.customerPhone
        this.detailedAdd = response.data.detailedAddress
        this.selectedOptions = [response.data.province.toString(), response.data.city.toString(), response.data.area.toString()]
        this.addGoodsList = response.data.goodsList;
        this.shipmentNo = response.data.shipmentNum;
        if (response.data.type == 2) {
          this.textStatus = true
          this.auditStatus = "发货出库"
        }
        if (response.data.type == 3) {
          this.textStatus = true
          this.auditStatus = "确认收货"
        }
      })
    },
    //查询客户列表
    selectCustomer() {
      listCustomer().then(response => {
        this.customerList = response.rows;
      });
    },
    //展开行状态
    getRowClassName({row, rowIndex}) {
      if (row.goodWarehouseList.length == 0) {
        return 'row-expand-cover';
      }
    },
    //返回上一页商品列表页
    close() {
      this.$router.go(-1)
    },
  }
}

</script>
<style>
.row-expand-cover .el-table__expand-icon {
  visibility: hidden;
}
</style>
